package com.yisu.expressway.network;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.application.ExApplication;
import com.yisu.expressway.login.LoginManager;
import com.yisu.expressway.login.UserInvalidHandler;
import com.yisu.expressway.utils.HBLog;

import org.json.JSONArray;
import org.json.JSONObject;


/**
 * Created by date13 on 14-10-13.
 */
public class ApiResult<T> {
    public static final int OK = 0;
    public static final int ERROR_CODE_DATA_NOT_FOUND = -1100; //数据格式
    public static final int ERROR_CODE_DATA_ERR = -10002; //数据格式
    public static final int ERROR_ACCOUNT_LOGIN_OTHER_DEVICE = 4002;//账号登录其他设备
    public static final String ERROR_DATA_FORMAT = "错误的数据格式";
    public Boolean success=true;
    private Integer code = OK;
    private String msg="";
    private T result;
    private String msgJson;
    private static Gson gson = new Gson();

    public ApiResult() {
        super();
        // TODO Auto-generated constructor stub
    }
  /*  public ZLApiResult(JSONObject jsonObject,TypeToken<T> typeToken) {
        this(jsonObject,1,typeToken);
    }*/

    public ApiResult(JSONObject jsonObject, TypeToken<T> typeToken) {
        super();
//        Log.d(ZLApiUtils.TAG, "call response:" + jsonObject.toString());
        try{
//            code =  OK;//jsonObject.getInt("errorCode");

            Log.w("OMG","==jsonObject==" + jsonObject);
            if (jsonObject.has("errorCode")){
                code = jsonObject.getInt("errorCode");
                if (code != OK) {

                    success = false;

                    if (jsonObject.has("errorMessage")) {
                        msg = jsonObject.getString("errorMessage");
                    }

                    if(code == ERROR_ACCOUNT_LOGIN_OTHER_DEVICE){
                        msg = ExApplication.getInstance().getResources().getString(R.string.account_sign_in_another_device);
                        new UserInvalidHandler().invalidHappens(msg);
                    }
//                    if(code == 100007 || code == 100000 || code == 100019 || code == 100009){
//                        // 用户已在其他设备登录, 未登录或登录超时，请重新登录, 用户已被冻结, 用户token缓存为空
//                        new UserInvalidHandler().invalidHappens(msg);
//                    }
                }




            }else{

            }

            if (code == OK){
//                if(jsonObject.has("token")) {
//                    String token = jsonObject.getString("token");
////                    ZLApiUtils.refreshToken(token);
//                }


                try {
                    Class type = typeToken.getRawType();
                    if (jsonObject.has("data") && Object.class != type) {

                        Object msgObject = null;
                        Object jsonType = jsonObject.get("data");
                        msgJson = jsonType.toString();

                            if (type == Boolean.class) {
                                msgObject = jsonObject.getBoolean("data");
                                result = (T) msgObject;
                            } else if (type == String.class) {
                                msgObject = jsonObject.getString("data");
                                if (msgObject != null && !"".equals(msgObject.toString()))
                                    result = (T) msgObject;
                            } else if (type == Integer.class) {
                                msgObject = jsonObject.getInt("data");
                                result = (T) msgObject;
                            } else if (type == Double.class) {
                                msgObject = jsonObject.getDouble("data");
                                result = (T) msgObject;
                            } else if (type == Long.class) {
                                msgObject = jsonObject.getLong("data");
                                result = (T) msgObject;
                            } else {
                                if (jsonType instanceof JSONObject) {
                                    msgObject = jsonObject.getJSONObject("data");
                                } else if (jsonType instanceof JSONArray) {
                                    msgObject = jsonObject.getJSONArray("data");
                                }

                                if (typeToken != null && msgObject != null) {
                                    result = gson.fromJson(msgObject.toString(), typeToken.getType());
                                }
                            }


//                    || jsonType instanceof Integer
//                            || jsonType instanceof Boolean
//                            || jsonType instanceof Double
//                            || jsonType instanceof Float

                        if (msgObject != null)
                            msgJson = msgObject.toString();

                    }
                }catch (Exception ex){
                  HBLog.e("ApiResult",ex.getMessage()) ;
                }

            }
        }catch (Exception e) {
            // TODO: handle exception
            code = ERROR_CODE_DATA_ERR;
            msg = ERROR_DATA_FORMAT;
        }

    }
    private T testDefault(String msg){
        Gson gson = new Gson();
        result =  gson.fromJson(msg,new TypeToken<T>(){}.getType());
        return result;
    }
    private T test(String msg,TypeToken<T> typeToken){
        Gson gson = new Gson();
        result =  gson.fromJson(msg,typeToken.getType());
        return result;
    }

    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getMsgJson() {
        return msgJson;
    }
}
